package it.espsoft.easysuperenalotto.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Utils {
	public static String normalizeString(String input){
		char stringC[] = input.trim().toCharArray();
		String result = "";
		for (char c : stringC) {
			int n = c;
			if(n==160){
				result+=" ";
			}else result+=c;
		}
		return result.replaceAll("\\s+", " ").replace("\n", "");
	}

    public static String convertStreamToString(InputStream is)
            throws IOException {
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {        
            return "";
        }
    }
	
    public static SimpleDateFormat getAppDateFormatter(){
    	String format = "dd/MM/yyyy";
    	return new SimpleDateFormat(format);
    }
    
    public static SimpleDateFormat getAppDateTimeFormatter(){
    	String format = "dd/MM/yyyy HH:mm:ss";
    	return new SimpleDateFormat(format);
    }

	public static String formatDate(Date data) {
		return getAppDateFormatter().format(data);
	}
	
	public static String formatDateTime(Date data) {
		return getAppDateTimeFormatter().format(data);
	}
	
	public static Date parseDate(String date){
		try {
			return getAppDateFormatter().parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date parseDateTime(String dateTime){
		try {
			return getAppDateTimeFormatter().parse(dateTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
